import datetime

import pygame

from lib.sittings import info, SCREENSIZE


def head_tail_pos(snakeheadgroup, snakegroup, snake, currentscore, screen, die):
    pos = snake.rect.topleft
    quit = False
    if pos[0] < 0:
        info("$" * 50)
        info(f"* :game over:  [{datetime.datetime.now()}] [head -> pos] quit")
        quit = die(currentscore, screen)
    if pos[0] >= SCREENSIZE[0]:
        info("$" * 50)
        info(f"* :game over:  [{datetime.datetime.now()}] [head -> pos] quit")
        quit = die(currentscore, screen)
    if pos[1] < 0:
        info("$" * 50)
        info(f"* :game over:  [{datetime.datetime.now()}] [head -> pos] quit")
        quit = die(currentscore, screen)
    if pos[1] >= SCREENSIZE[1]:
        info("$" * 50)
        info(f"* :game over:  [{datetime.datetime.now()}] [head -> pos] quit")
        quit = die(currentscore, screen)

    # collisions
    # head -> tail
    col = pygame.sprite.groupcollide(snakeheadgroup, snakegroup, False, False)
    for head in col:
        for tail in col[head]:
            if not tail is snake:
                info("$" * 50)
                info(f"* :game over:  [{datetime.datetime.now()}] [head -> tail] quit")
                quit = die(currentscore, screen)
                break
    return quit


def head_blocks(snakeheadgroup, blockgroup, currentscore, screen, die):
    quit = False
    col = pygame.sprite.groupcollide(snakeheadgroup, blockgroup, False, False)
    for head in col:
        info(f"[{datetime.datetime.now()}] head -> blocks")
        for collidedblock in col[head]:
            info("$" * 50)
            info(f"* :game over:  [{datetime.datetime.now()}] [head -> blocks] quit")
            return die(currentscore, screen)


